package com.mq.basic;

import com.mq.ConnectionUtils;
import com.rabbitmq.client.AMQP.BasicProperties;
import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * 简单队列——消费者
 * 
 * @author Administrator
 *
 */
public class Recver {
	/**
	 * 队列名称，和生产者的队列名称必须保持一致
	 */
	private static final String QUEUE = "test_simple_queue";
 
	public static void main(String[] args) throws IOException, TimeoutException {
		// 获取连接
		Connection con = ConnectionUtils.getConnection();
		// 从连接中创建通道
		Channel channel = con.createChannel();
		// 声明队列
		channel.queueDeclare(QUEUE, false, false, false, null);
		// 创建消费者
		Consumer consumer = new DefaultConsumer(channel) {
                        // 获取消息
			@Override
			public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body)
					throws IOException {
				String msg = new String(body, "utf-8");
				System.out.println("接收到消息——" + msg);
			}
 
		};
		// 监听队列
		channel.basicConsume(QUEUE, true, consumer);
	}
}
